//
//  AppDelegate.m
//  JG
//
//  Created by 程龙 on 2023/7/27.
//

#import "AppDelegate.h"
#import "UIView+Extension.h"
#import "CLHomeViewController.h"
#import "CLMainViewController.h"
#import "WWKApi.h"
@interface AppDelegate ()<WWKApiDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [CLRequestTool setupPublicParameters];
    [GKConfigure setupDefaultConfigure];
    //正式环境
    [WWKApi registerApp:@"wwauth4cd146e6324cc102000164" corpId:@"wx4cd146e6324cc102" agentId:@"1000164"];
    //测试环境
//    [WWKApi registerApp:@"wwauth835eb9342a678d26000157" corpId:@"ww835eb9342a678d26" agentId:@"1000157"];

    
    //定位服务 高德 个人
//    [AMapServices sharedServices].apiKey =@"4e1039c495040b500576decd725a998d";
    
    //企业
    [AMapServices sharedServices].apiKey =@"84d4d31d69641f8f54a36c0428b94ab7";
    
    [AMapLocationManager updatePrivacyAgree:AMapPrivacyAgreeStatusDidAgree];
    [AMapLocationManager updatePrivacyShow:AMapPrivacyShowStatusDidShow privacyInfo:AMapPrivacyInfoStatusDidContain];


    
    // 配置导航栏属性
    [GKConfigure setupCustomConfigure:^(GKNavigationBarConfigure * _Nonnull configure) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, GK_STATUSBAR_NAVBAR_HEIGHT)];
        // 导航栏标题颜色
        configure.titleColor = [UIColor whiteColor];
        // 导航栏标题字体
        configure.titleFont = [UIFont systemFontOfSize:18.0f];
        // 导航栏返回按钮样式
        configure.backStyle = GKNavigationBarBackStyleWhite;
        // 设置状态栏样式
        configure.statusBarStyle = UIStatusBarStyleLightContent;
        // 导航栏左右item间距
        configure.gk_navItemLeftSpace = 10.0f;
        configure.gk_navItemRightSpace = 10.0f;
        configure.gk_restoreSystemNavBar = YES;
        
        
    }];
    // 设置手势返回
    [GKGestureConfigure setupCustomConfigure:^(GKGestureHandleConfigure * _Nonnull configure) {
        configure.gk_scaleX = 0.90;
        configure.gk_scaleY = 0.92;
        configure.gk_openScrollViewGestureHandle = YES;
    }];
    
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    UINavigationController *nav = [UINavigationController rootVC:[CLHomeViewController new]];
    self.window.rootViewController = nav;
     
    [self.window makeKeyAndVisible];
    
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(){}];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
}


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [self handleOpenURL:url sourceApplication:nil];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation {
    return [self handleOpenURL:url sourceApplication:sourceApplication];
}

- (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication {
    /*! @brief 处理外部调用URL的时候需要将URL传给SDK进行相关处理
     * @param url 外部调用传入的url
     * @param delegate 当前类需要实现WWKApiDelegate对应的方法
     */
    return [WWKApi handleOpenURL:url delegate:self];
}
// 如果第三方程序向企业微信发送了sendReq的请求，那么onResp会被回调。sendReq请求调用后，会切到企业微信终端程序界面。
- (void)onResp:(WWKBaseResp *)resp {
    /*! @brief 所有通过sendReq发送的SDK请求的结果都在这个函数内部进行异步回调
     * @param resp SDK处理请求后的返回结果 需要判断具体是哪项业务的回调
     */
    NSMutableString *extra = [NSMutableString string];
    /* SSO的回调 */
    if ([resp isKindOfClass:[WWKSSOResp class]]) {
        WWKSSOResp *r = (WWKSSOResp *)resp;
        [extra appendFormat:@"%@ for %@", r.code, r.state];
        
        if([NSString stringWithFormat:@"%@",r.code].length == 0){
            [CLTool cl_showFail:@"授权失败" block:^{
                            
            }];
            return;
        }

        [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
            request.path=url_token;
            request.methodType=ZBMethodTypePOST;
        }  success:^(id responseObject,ZBURLRequest * request){
            if ([[NSString stringWithFormat:@"%@",responseObject[@"code"]] isEqualToString:@"200"]) {
                [CLData sharedClass].token = responseObject[@"data"][@"token"];
                
                [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
                    request.path=url_code;
                    request.methodType=ZBMethodTypePOST;
                    request.parameters = @{@"code":r.code};
                    request.userInfo = @{@"type":@"999"};
                }  success:^(id responseObject,ZBURLRequest * request){
                    if ([[NSString stringWithFormat:@"%@",responseObject[@"code"]] isEqualToString:@"200"]) {
                        
                        [CLData sharedClass].jobNumber = responseObject[@"msg"];
                        
                        [CLTool cl_saveUser:[CLData sharedClass].jobNumber key:@"jobNumber"];
                        
                        CLLog(@"工号为：%@",[CLData sharedClass].jobNumber);
                        
                        
                        [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
                            request.path=url_login;
                            request.methodType=ZBMethodTypePOST;
                            request.parameters = @{@"workCode":[CLData sharedClass].jobNumber};
                            request.userInfo = @{@"type":@"999"};
                        }  success:^(id responseObject,ZBURLRequest * request){

                            if ([[NSString stringWithFormat:@"%@",responseObject[@"code"]] isEqualToString:@"200"]) {
                                [CLData sharedClass].homeUrl = responseObject[@"data"][@"url"];
                                
                                CLMainViewController *vc = [[CLMainViewController alloc]init];
                                vc.address = [CLData sharedClass].homeUrl;
                                [[CLTool getCurrentVC].navigationController pushViewController:vc animated:YES];
                            }else{
                                [CLTool cl_showFail:responseObject[@"msg"] block:^{
                                                
                                }];
                            }



                        } failure:^(NSError * _Nullable error) {
                            
                        } finished:^(id responseObject, NSError *error, ZBURLRequest *request) {
                            NSLog(@"请求完成");
                        }];
                    }else{
                        [CLTool cl_showFail:responseObject[@"msg"] block:^{
                                        
                        }];
                    }


                    
                } failure:^(NSError * _Nullable error) {
                    [CLTool cl_showFail:@"请求错误" block:^{
                        
                    }];
                } finished:^(id responseObject, NSError *error, ZBURLRequest *request) {
                    NSLog(@"请求完成");
                }];
                
            }else{
                [CLTool cl_showFail:responseObject[@"msg"] block:^{
                    
                }];
            }
        } failure:^(NSError * _Nullable error) {
            [CLTool cl_showFail:@"请求错误" block:^{
                
            }];
        } finished:^(id responseObject, NSError *error, ZBURLRequest *request) {
            NSLog(@"请求完成");
        }];
        

    }
    
//    if (extra.length) [extra insertString:@"\n\n" atIndex:0];
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"返回结果" message:[NSString stringWithFormat:@"错误码：%d\n错误信息：%@%@", resp.errCode, resp.errStr, extra] preferredStyle:UIAlertControllerStyleAlert];
//    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
//    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alert animated:YES completion:nil];
}
// onReq是企业微信终端向第三方程序发起请求，要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时，会切回到企业微信终端程序界面。
-(void) onReq:(WWKBaseReq*)req {
    
}
//- (void)applicationWillTerminate:(UIApplication*)application
//
//{
//
//NSLog(@"程序被杀死，applicationWillTerminate");
//
//}



@end
